<?php
/**
* SocialEngine
*
* @category   Application_Extensions
* @package    Birthdayemail
* @copyright  Copyright 2009-2010 BigStep Technologies Pvt. Ltd.
* @license    http://www.socialengineaddons.com/license/
* @version    $Id: install.php 6590 2010-17-11 9:40:21Z SocialEngineAddOns $
* @author     SocialEngineAddOns
*/
class Birthdayemail_Installer extends Engine_Package_Installer_Module
{

  function onInstall() {

     // Insert the 
    $db     = $this->getDb();

    $select = new Zend_Db_Select($db);
    $select
        ->from('engine4_core_modules')
        ->where('name = ?', 'core')
        ->limit(1);
    $core_version =  $select->query()->fetchAll();
    if ( !empty($core_version) ) {
			$version = $core_version[0]['version'];
			if( $version < '4.1.0' ) {
				$select = new Zend_Db_Select($db);
				$select
						->from('engine4_core_tasks')
						->where('plugin = ?', 'Birthdayemail_Plugin_Task_ReminderMail')
						->where('title = ?', 'Birthday Reminder')
						->limit(1);
				$task = $select->query()->fetchAll();
				if (empty($task)) {
					$db->insert('engine4_core_tasks', array(
					'title' => "Birthday Reminder",
					'category' => 'system',
					'module' => 'birthdayemail',
					'system' => '1',
					'plugin' => "Birthdayemail_Plugin_Task_ReminderMail",
					'timeout' => "86400",
					'type' => 'automatic',
					'state' => 'dormant',
					'data' => NULL,
					'enabled' => "1",
					'executing' => "0",
					'executing_id' => "0",
					'started_last' => "0",
					'started_count' => "0",
					'completed_last' => "0",
					'completed_count' => "0",
					'failure_last' => "0",
					'failure_count' => "0",
					'success_last' => "0",
					'success_count' => "0",
				));
				}
				// Insert the 
				$select = new Zend_Db_Select($db);
				$select
						->from('engine4_core_tasks')
						->where('plugin = ?', 'Birthdayemail_Plugin_Task_WishMail')
			->where('title = ?', 'Birthday Wish')
						->limit(1);
				$task = $select->query()->fetchAll();
				if (empty($task)) {
					$db->insert('engine4_core_tasks', array(
					'title' => "Birthday Wish",
					'category' => 'system',
					'module' => 'birthdayemail',
					'system' => '1',
					'plugin' => "Birthdayemail_Plugin_Task_WishMail",
					'timeout' => "86400",
					'type' => 'automatic',
					'state' => 'dormant',
					'data' => NULL,
					'enabled' => "1",
					'executing' => "0",
					'executing_id' => "0",
					'started_last' => "0",
					'started_count' => "0",
					'completed_last' => "0",
					'completed_count' => "0",
					'failure_last' => "0",
					'failure_count' => "0",
					'success_last' => "0",
					'success_count' => "0",
				));
				}
			}else {
				$select = new Zend_Db_Select($db);
				$select
						->from('engine4_core_tasks')
						->where('plugin = ?', 'Birthdayemail_Plugin_Task_ReminderMail')
						->where('title = ?', 'Birthday Reminder')
						->limit(1);
				$task = $select->query()->fetchAll();
				if (empty($task)) {
					$db->insert('engine4_core_tasks', array(
					'title' => "Birthday Reminder",
					'module' => 'birthdayemail',
					'plugin' => "Birthdayemail_Plugin_Task_ReminderMail",
					'timeout' => "86400",
					'processes' => "1",
					'semaphore' => "0",
					'started_last' => "0",
					'started_count' => "0",
					'completed_last' => "0",
					'completed_count' => "0",
					'failure_last' => "0",
					'failure_count' => "0",
					'success_last' => "0",
					'success_count' => "0",
				));
				}

				// Insert the 
				$select = new Zend_Db_Select($db);
				$select
						->from('engine4_core_tasks')
						->where('plugin = ?', 'Birthdayemail_Plugin_Task_WishMail')
			->where('title = ?', 'Birthday Wish')
						->limit(1);
				$task = $select->query()->fetchAll();
				if (empty($task)) {
					$db->insert('engine4_core_tasks', array(
					'title' => "Birthday Wish",
					'module' => 'birthdayemail',
					'plugin' => "Birthdayemail_Plugin_Task_WishMail",
					'timeout' => "86400",
					'processes' => "1",
					'semaphore' => "0",
					'started_last' => "0",
					'started_count' => "0",
					'completed_last' => "0",
					'completed_count' => "0",
					'failure_last' => "0",
					'failure_count' => "0",
					'success_last' => "0",
					'success_count' => "0",
				));
				}
			}
		}
    parent::onInstall();
  }

  function onEnable()
  {
     $db = $this->getDb();

		$select = new Zend_Db_Select($db);
		$select
				->from('engine4_core_menuitems')
				->where('name = ?', 'birthdayemail_admin_main_settings')
				->limit(1);
		$task = $select->query()->fetchAll();
		if( empty($task) ) {
			$db->insert('engine4_core_menuitems', array(
				'name' => "birthdayemail_admin_main_settings",
				'module' => "birthday",
				'label' => "Email Settings",
				'plugin' => "",
				'params' => '{"route":"admin_default","module":"birthdayemail","controller":"settings"}',
				'menu' => "birthday_admin_main",
				'submenu' => "",
				'order' => "2",
			));
		}
    parent::onEnable();
  }

  function onDisable()
  {
    $db = $this->getDb();

		$select = new Zend_Db_Select($db);
		$select
				->from('engine4_core_menuitems')
				->where('name = ?', 'birthdayemail_admin_main_settings')
				->limit(1);
		$task = $select->query()->fetchAll();
		if( !empty($task) ) {
			$db->delete('engine4_core_menuitems', array(
				'name = ?' => "birthdayemail_admin_main_settings"
			));
		}

    parent::onDisable();
  } 
}
?>